package listnode;

import listnode.data.GenerateRandomListNode;
import listnode.data.ListNode;

/**
 * 环形链表
 *
 * 快慢指针
 */
public class HasCycler {

    public boolean hasCycle(ListNode head) {

        if(head==null||head.next==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head.next;
        while(slow!=fast){
            if (fast==null||fast.next==null){
                return false;
            }
            slow=slow.next;
            fast=fast.next.next;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println("Hello and welcome!");
        HasCycler s = new HasCycler();
        System.out.println(s.hasCycle(GenerateRandomListNode.generateListWithPossibleCycle(10,1,100)));
        System.out.println(s.hasCycle(GenerateRandomListNode.generateListWithPossibleCycle(10,1,100)));
        System.out.println(s.hasCycle(GenerateRandomListNode.generateListWithPossibleCycle(10,1,100)));
        System.out.println(s.hasCycle(GenerateRandomListNode.generateListWithPossibleCycle(10,1,100)));
    }
}
